home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / eflibpt4.zip / DEMO / DATATYPE / HASHTABL.PAS < prev    next >
Pascal/Delphi Source File  |  1996-08-18  |  3KB  |  73 lines

  1. { Borland Pascal Extended Function Library - EFLIB (C) Johan Larsson, 1996
  2.   Demonstration; simple hash table built of text file
  3.  
  4.   EFLIB IS PROTECTED BY THE COPYRIGHT LAW AND MAY NOT BE COPIED, SOLD OR
  5.   MANIPULATED. FOR MORE INFORMATION, SEE PROGRAM MANUAL! THIS DEMONSTRAT-
  6.   ION PROGRAM MAY FREELY BE USED AND DISTRIBUTED.                          }
  7.  
  8.  
  9. uses EFLIBDEF, EFLIBINI, EFLIBBAS, EFLIBTXT, EFLIBDAT;
  10.  
  11.  
  12. const CapacityOfElements = 1500; { Maximum capacity }
  13.  
  14. var MyHashTable : HashTableObjectType; MyText : string[30];
  15.     Index : word; Timer : TimerObjectType; TextFile : text;
  16.  
  17. begin
  18.      Timer.Initialize; Index := 0;
  19.  
  20.      with MyHashTable do begin
  21.  
  22.           { Initialize hash table }
  23.           Initialize (CapacityOfElements, SizeOf(MyText));
  24.  
  25.           WriteLn ('Building hash table ... ');
  26.  
  27.  
  28.           { Insert data in hash table }
  29.  
  30.           Assign (TextFile, '..\DATA.TXT');
  31.           System.Reset (TextFile);
  32.  
  33.           while not EOF(TextFile) do begin
  34.               { Clear text variable }
  35.               FillChar (MyText, SizeOf(MyText), 0);
  36.               { Get text line from file }
  37.               ReadLn (TextFile, MyText);
  38.               { Add text to hash table if it not already exist }
  39.               if Search(MyText) = 0 then Add (MyText);
  40.               Inc (Index);
  41.           end;
  42.  
  43.           { Verify that all elements was added correctly }
  44.           if Index = Elements then WriteLn ('Hash table was correctly built.');
  45.  
  46.           { Resize and rebuild hash table; all hash codes will be
  47.             recalculated }
  48.           Resize(CapacityOfElements * 2); { Double the size }
  49.  
  50.           { Compare hash table data with original file lines }
  51.           Assign (TextFile, '..\DATA.TXT');
  52.           System.Reset (TextFile);
  53.  
  54.           while not EOF(TextFile) do begin
  55.               { Clear text variable }
  56.               FillChar (MyText, SizeOf(MyText), 0);
  57.               { Get text line from file }
  58.               ReadLn (TextFile, MyText);
  59.  
  60.               { Search for line two times and return text and index }
  61.               Write (String(ElementPointer(Search(MyText))^) + ' (' +
  62.                      StringNumber(Search(MyText), 4, 0) + ') = ' + MyText:40);
  63.           end;
  64.  
  65.           WriteLn (Elements, ' table elements used (maximum ', Capacity,').');
  66.  
  67.           Intercept; { Hash table }
  68.      end;
  69.  
  70.      if GlobalDataError then WriteLn ('Hashing failed - duplicate insertion?');
  71.  
  72.      WriteLn (Timer.StringMS);
  73. end.